package com.yupi.yuoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuoj.annotation.AuthCheck;
import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.constant.UserConstant;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.contentrecommendation.ContentRecommendationQueryRequest;
import com.yupi.yuoj.model.entity.ContentRecommendation;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.model.vo.ContentRecommendationVO;
import com.yupi.yuoj.service.ContentRecommendationService;
import com.yupi.yuoj.service.UserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 内容推荐接口
 *
 * @author jingjin
 */
@RestController
@RequestMapping("/content_recommendation")
@Slf4j
public class ContentRecommendationController {

    @Resource
    private ContentRecommendationService contentRecommendationService;

    @Resource
    private UserService userService;

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<ContentRecommendation> getContentRecommendationById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ContentRecommendation contentRecommendation = contentRecommendationService.getById(id);
        if (contentRecommendation == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(contentRecommendation);
    }

    /**
     * 分页获取内容推荐列表（仅管理员）
     *
     * @param contentRecommendationQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<ContentRecommendation>> listContentRecommendationByPage(@RequestBody ContentRecommendationQueryRequest contentRecommendationQueryRequest) {
        long current = contentRecommendationQueryRequest.getCurrent();
        long size = contentRecommendationQueryRequest.getPageSize();
        Page<ContentRecommendation> contentRecommendationPage = contentRecommendationService.page(new Page<>(current, size),
                contentRecommendationService.getQueryWrapper(contentRecommendationQueryRequest));
        return ResultUtils.success(contentRecommendationPage);
    }

    /**
     * 分页获取内容推荐列表（封装类）
     *
     * @param contentRecommendationQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<ContentRecommendationVO>> listContentRecommendationVOByPage(@RequestBody ContentRecommendationQueryRequest contentRecommendationQueryRequest,
            HttpServletRequest request) {
        long current = contentRecommendationQueryRequest.getCurrent();
        long size = contentRecommendationQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<ContentRecommendation> contentRecommendationPage = contentRecommendationService.page(new Page<>(current, size),
                contentRecommendationService.getQueryWrapper(contentRecommendationQueryRequest));
        return ResultUtils.success(contentRecommendationService.getContentRecommendationVOPage(contentRecommendationPage, request));
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<ContentRecommendationVO> getContentRecommendationVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ContentRecommendation contentRecommendation = contentRecommendationService.getById(id);
        if (contentRecommendation == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(contentRecommendationService.getContentRecommendationVO(contentRecommendation, request));
    }

    /**
     * 获取当前用户的推荐内容列表
     *
     * @param contentRecommendationQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<ContentRecommendationVO>> listMyContentRecommendationVOByPage(@RequestBody ContentRecommendationQueryRequest contentRecommendationQueryRequest,
            HttpServletRequest request) {
        if (contentRecommendationQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        contentRecommendationQueryRequest.setUserId(loginUser.getId());
        long current = contentRecommendationQueryRequest.getCurrent();
        long size = contentRecommendationQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<ContentRecommendation> contentRecommendationPage = contentRecommendationService.page(new Page<>(current, size),
                contentRecommendationService.getQueryWrapper(contentRecommendationQueryRequest));
        return ResultUtils.success(contentRecommendationService.getContentRecommendationVOPage(contentRecommendationPage, request));
    }

}